package web.servlet;

import base.BaseServlet;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import dao.iml.CourseContentDao;
import dao.iml.CourseContentDaoImpl;
import org.apache.commons.beanutils.BeanUtils;
import pojo.Course;
import pojo.Course_Lesson;
import pojo.Course_Section;
import service.iml.ServiceDao;
import service.iml.ServiceDaoImpl;
import utils.DateUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

@WebServlet("/courseContent")
public class CourseContentServlet extends BaseServlet {
    ServiceDao sd = new ServiceDaoImpl();

    public void findSectionAndLessonByCourseId(HttpServletRequest request, HttpServletResponse response){
        try {
            String id = request.getParameter("id");
            List<Course_Section> sectionCourseId = sd.findSectionCourseId(Integer.parseInt(id));
            String result = JSON.toJSONString(sectionCourseId);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void findCourseById(HttpServletRequest request, HttpServletResponse response){
        try {
            String course_id = request.getParameter("course_id");
            CourseContentDao ccd = new CourseContentDaoImpl();
            Course course = ccd.findCourseById(Integer.parseInt(course_id));
            SimplePropertyPreFilter sppf = new SimplePropertyPreFilter(Course.class,"course_name");
            String result = JSON.toJSONString(course, sppf);
            response.getWriter().print(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void saveOrUpdateSection(HttpServletRequest request, HttpServletResponse response){
        try {
            Course_Section cs = new Course_Section();
            String id = request.getParameter("id");
            String status = request.getParameter("status");
            if (id != null){
                //修改操作
                cs.setId(Integer.parseInt(id));
                cs.setStatus(Integer.parseInt(status));
                String result = sd.updateSectionStatus(cs);
                response.getWriter().print(result);
            }else {
                //新建操作
                String course_id = request.getParameter("course_id");
                String section_name = request.getParameter("section_name");
                String description = request.getParameter("description");
                String order_num = request.getParameter("order_num");

                cs.setCourse_id(Integer.parseInt(course_id));
                cs.setSection_name(section_name);
                cs.setDescription(description);
                cs.setOrder_num(Integer.parseInt(order_num));
                cs.setUpdate_time(DateUtils.getDateFormart());
                cs.setCreate_time(DateUtils.getDateFormart());
                String result = sd.saveSection(cs);
                response.getWriter().print(result);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    public void saveOrUpdateLesson(HttpServletRequest request, HttpServletResponse response){
        try {
            Map<String,Object> map = (Map) request.getAttribute("map");
            Course_Lesson cl = new Course_Lesson();
            BeanUtils.populate(cl,map);
            if (map.get("id") != null){
                //修改操作！
                String result = sd.updateLesson(cl);
                response.getWriter().print(result);
            }else {
                String result = sd.saveLesson(cl);
                response.getWriter().print(result);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
